package controller.parameterpanels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.jfree.ui.RefineryUtilities;

import model.IManufacturingMethod;
import model.auxillary.Parameter;
import model.auxillary.ParameterCollection;
import model.auxillary.ParameterCollectionScaling;
import model.scaling.ICalculatableScaling;
import model.scaling.ICompany;
import model.scaling.IEBenchmarkScaling;
import model.scaling.IFacility;
import model.scaling.IFactory;
import model.scaling.IIndustrialSector;
import model.scaling.ILocation;
import model.scaling.IProduct;
import view.BarChartFrame;
import view.CenterTabbedPane.EBenchmarkTab;
import view.EBenchmarkPanel;
import view.parameterpanels.ScalingResultPanel;
import controller.CtrInformationPanel;
import controller.IControllable;

//#Neu
/**
 * The class {@code CtrScalingResultPanel} controls the behavior the scaling
 * result panel. 
 * @author Sebastian Perlwitz
 *
 */
public class CtrScalingResultPanel implements IControllable {
	
	/**
	 * The information panel controller.	
	 */
	private CtrInformationPanel infoPanelCtr;
	
	/**
	 * The controller's view.
	 */
	private ScalingResultPanel scalingResultPanel;
	
	/**
	 * The calculation model (of all scaling classes) of the controller.
	 */
	private ICalculatableScaling calcModelScaling;
	
	/**
	 * The eBenchmarkScaling calculation model of the controller.
	 */
	private IEBenchmarkScaling eBenchmarkScaling;
	
	/**
	 * To create a new {@code CtrScalingResultPanel}.
	 * @param calcModelScaling the calculation model connected to the 
	 * controller.
	 * @param infoPanelCtr the information panel controller.
	 * @param logoPath a path to an optional logo that is shown on the
     * parameter panel.
	 */
	public CtrScalingResultPanel(
    		ICalculatableScaling calcModelScaling,
            CtrInformationPanel infoPanelCtr,
            String logoPath) {
		this.eBenchmarkScaling = (IEBenchmarkScaling) calcModelScaling;
        this.calcModelScaling = calcModelScaling;
        this.infoPanelCtr = infoPanelCtr;
        
        // Register the listener of the visualize button.
        this.infoPanelCtr.registerVisualizeListener(createVisualizeListener()); 
        
        createView(logoPath);        
    }
	
	@Override
    public void createView(String logoPath) {    	
    	scalingResultPanel = new ScalingResultPanel(
    			logoPath,
    			EBenchmarkTab.SHOW_SCALING_RESULTS,
    			"Gesamtergebnisse Skalierung");
    }

	@Override
	public EBenchmarkPanel getView() {
		return scalingResultPanel;
	}
	
	/**
     * To add parameter panels to the controller's view.
     */
    public void addParameters() {
    	
    	// Remove formerly created parameter informations.
    	scalingResultPanel.getParameterPanel().removeAll();
    	
    	List<ParameterCollectionScaling> scalingResults = 
    			calcModelScaling.getResults();
    	
    	// Iterate through scalingResults in order to get access
    	// to each collection within scalingResults.
    	for(ParameterCollectionScaling collection : scalingResults) {
    		
    		List<String[]> paramsInfo = new ArrayList<String[]>();
    		
    		// Iterate through the collection in order to get access
        	// to each parameter within the collection.
    		for(Parameter param : collection.getParameters()) {
    			
    			// Fetch the interesting parameter information.
    			String[] paramInfo = new String[4];
    			paramInfo[0] = param.getName();
    			paramInfo[1] = param.getGuiAbbreviation();
    			paramInfo[2] = param.getUnit();
    			
    			if(Double.isNaN(param.getValue())) {
    				paramInfo[3] = "";
    			} else {
    				paramInfo[3] = String.valueOf(
    						Math.round(param.getValue() * 100000000.0) /
    						100000000.0);
    			}
    			paramsInfo.add(paramInfo);    			
    		}
    		scalingResultPanel.addParameterSet(paramsInfo,
    				collection.getName(), collection.getScalingID());    		
    	}
    }
    
    /**
     * To deselect all JCheckBoxes in the view.
     */
    public void deselectCheckBoxes() {
    	scalingResultPanel.deselectAllCheckBoxes();
    }
    
    /**
     * To create a listener for the button "Visualisieren".
     * @return The button's listener.
     */
    private ActionListener createVisualizeListener() {
    	return new ActionListener() {
    		@Override
            public void actionPerformed(ActionEvent e) {
    			createBarCharts();
            	scalingResultPanel.deselectAllCheckBoxes();
            }
		};
    }
    
    /**
     * To create the bar charts that show the results of all 
     * selected check boxes.
     */
    private void createBarCharts() {
    	
    	// Two maps that are used to save bar chart data and titles.
    	Map<String, ArrayList<ArrayList<String[]>>> dataBarChart = 
    			new HashMap<String, ArrayList<ArrayList<String[]>>>();
    	Map<String, String> barChartTitles = new HashMap<String, String>();
    	
    	// Get all selected check boxes.
    	ArrayList<String> listScalingResultIDs = 
    			scalingResultPanel.getSelectedCheckBoxIDs();
    	
    	// Each check box is linked to an scalingID. Iterate through all IDs.
    	int i = 0;
    	for(String scalingResultID : listScalingResultIDs) {
    		
    		// To each check box two bar charts and a bar chart title 
    		// are created.
    		ArrayList<String[]> dataBarChartOne = 
    				getDataBarChartOne(scalingResultID);
    		ArrayList<String[]> dataBarChartTwo = 
        			getDataBarChartTwo(scalingResultID);
    		String titleBarChart = getBarChartTitle(scalingResultID);
        	
    		// Save the data of the two bar charts.
        	ArrayList<ArrayList<String[]>> dataOneTwo = 
        			new ArrayList<ArrayList<String[]>>(2);
        	
        	dataOneTwo.add(0, dataBarChartOne);
        	dataOneTwo.add(1, dataBarChartTwo);
        	
        	// Add the data and title to the corresponding map.
        	String I = String.valueOf(i);
        	dataBarChart.put(I, dataOneTwo);
        	barChartTitles.put(I, titleBarChart);
        	i++;
    	}
    	
    	// Create the new Frame containing all bar charts.
    	BarChartFrame barChart = new BarChartFrame(barChartTitles, dataBarChart);
		barChart.pack();
        RefineryUtilities.centerFrameOnScreen(barChart);
        barChart.setVisible(true);
    }
    
    /**
     * Creates a bar chart title for the scalingResultID.
     * @param scalingResultID the scalingID belonging to the selected check box.
     * @return The bar chart title.
     */
    private String getBarChartTitle(String scalingResultID) {
    	
    	String titleBarChart= new String();
    	
    	IFacility facility = null;
		IFactory factory = null;
		ILocation location = null;
		ICompany company = null;
		IIndustrialSector industry = null;
		IProduct product = null;
		
		// Try to get the scaling level to which this scalingResultID 
		// belongs and create the title of the bar chart.
		try {
			facility = eBenchmarkScaling.getFacility(scalingResultID);
			titleBarChart = "REE von " + facility.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			factory = eBenchmarkScaling.getFactory(scalingResultID);
			titleBarChart = "REE von " + factory.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			location = eBenchmarkScaling.getLocation(scalingResultID);
			titleBarChart = "REE von " + location.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			company = eBenchmarkScaling.getCompany(scalingResultID);
			titleBarChart = "REE von " + company.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			industry = eBenchmarkScaling.getIndustrialSector(
					scalingResultID);
			titleBarChart = "REE von " + industry.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			product = eBenchmarkScaling.getProduct(scalingResultID);
			titleBarChart = "REE von " + product.getName() + " im Vergleich";
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		return titleBarChart;
	}

	/**
     * To create the parameter information used for the first bar chart. In
     * this bar chart the result of the scalingID is compared to all results
     * that are assigned to this scalingID.
     * For example if the scalingID belongs to a facility, then this facility 
     * is compared to all manuMethods that are linked to it.
     * @param scalingResultID the scalingID belonging to the selected check box.
     * @return <b>dataBarChartOne</b> a list of Strings with the size 2 called 
     * paramInfo:<br> 
     * dataBarChartOne[0] = paramInfo:<br>
     * paramInfo[0] = name of the manuMethod, facility, factory,...<br>
     * paramInfo[1] = value of the parameter.
     */
    private ArrayList<String[]> getDataBarChartOne(String scalingResultID) {
    	
    	ArrayList<String[]> dataBarChartOne = new ArrayList<String[]>();
    	
		IFacility facility = null;
		IFactory factory = null;
		ILocation location = null;
		ICompany company = null;
		IIndustrialSector industry = null;
		IProduct product = null;
		
		// Try to get the scaling level to which this scalingResultID 
		// belongs.
		try {
			facility = eBenchmarkScaling.getFacility(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			factory = eBenchmarkScaling.getFactory(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			location = eBenchmarkScaling.getLocation(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			company = eBenchmarkScaling.getCompany(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			industry = eBenchmarkScaling.getIndustrialSector(
					scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			product = eBenchmarkScaling.getProduct(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		
		// Test all scaling levels and find out to which this scalingID 
		// belongs. Then add the appropriate parameter information.
		if(facility != null) {
			
			List<ParameterCollectionScaling> results = 
					facility.getResults();
        	
        	// Iterate through results in order to get access to each 
			// collection within results.
        	for(ParameterCollectionScaling collection : results) {
        		
        		// Get the parameter of the scaling level. In this case the
    			// parameter of the facility. Therefore iterate through the 
    			// collection to get access to each parameter.
    			for(Parameter param : collection.getParameters()) {
    				String[] paramInfo = new String[2];
    				
    				// Add the facility name.
    				paramInfo[0] = facility.getName();
    				
    				// Add the value of the facility result.
    				if(Double.isNaN(param.getValue())) {
    					paramInfo[1] = "";
    				} else {
    					paramInfo[1] = String.valueOf(
    							Math.round(param.getValue() * 
    									100000000.0) / 100000000.0);
    				}
    				dataBarChartOne.add(paramInfo);
    			}
        	}
        	
        	// Get the manufacturing methods that have been assigned to this 
			// facility.
			ArrayList<IManufacturingMethod> manuMethodsOfFacility = 
					facility.getManuMethods();
			
			// Iterate through all manuMethods that belong to this facility.
    		for(IManufacturingMethod manuMethodOfFacility : 
    				manuMethodsOfFacility) {
    			
				List<ParameterCollection> manuMethodResults = 
						manuMethodOfFacility.getResults();
				
				// Iterate through the results in order to get access
            	// to each collection within the results.
            	for(ParameterCollection collectionManuMethod : 
            		manuMethodResults) {
            		
            		// Iterate through the collection in order to get access
                	// to each parameter within the collection.
            		for(Parameter param : 
            			collectionManuMethod.getParameters()) {
            			
            			// Only get the physical REE of the manuMethod.
            			if(param.getAbbreviation().equals("eta_phy")) {
            				String[] paramInfo = new String[2];
            				
            				// Add the name of the manuMethod.
                			paramInfo[0] = manuMethodOfFacility.getName();
                			
                			// Add the value of the physical REE.
                			if(Double.isNaN(param.getValue())) {
                				paramInfo[1] = "";
                			} else {
                				paramInfo[1] = String.valueOf(
                						Math.round(param.getValue() * 
                								100000000.0) / 100000000.0);
                			}
                			dataBarChartOne.add(paramInfo);
            			}
            		}
            	}
			}
		} else if(factory != null) {
			List<ParameterCollectionScaling> results = 
					factory.getResults();
			for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = factory.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartOne.add(paramInfo);
				}
			}
			ArrayList<IFacility> facilitiesOfFactory = factory.getFacilities();
    		for(IFacility facilityOfFactory : facilitiesOfFactory) {
				List<ParameterCollectionScaling> facilityResults = 
						facilityOfFactory.getResults();
				for(ParameterCollectionScaling collectionFacility : 
            		facilityResults) {
            		for(Parameter param : collectionFacility.getParameters()) {
            			String[] paramInfo = new String[2];
                		paramInfo[0] = facilityOfFactory.getName();
            			if(Double.isNaN(param.getValue())) {
            				paramInfo[1] = "";
            			} else {
            				paramInfo[1] = String.valueOf(
            						Math.round(param.getValue() * 
            								100000000.0) / 100000000.0);
            			}
            			dataBarChartOne.add(paramInfo);
            		}
            	}
			}
		} else if (location != null) {
			List<ParameterCollectionScaling> results = 
					location.getResults();
			for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = location.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartOne.add(paramInfo);
				}
			}
			ArrayList<IFactory> factoriesOfLocation = location.getFactories();
    		for(IFactory factoryOfLocation : factoriesOfLocation) {
				List<ParameterCollectionScaling> factoryResults = 
						factoryOfLocation.getResults();
				for(ParameterCollectionScaling collectionFactory : 
            		factoryResults) {
            		for(Parameter param : collectionFactory.getParameters()) {
            			String[] paramInfo = new String[2];
                		paramInfo[0] = factoryOfLocation.getName();
            			if(Double.isNaN(param.getValue())) {
            				paramInfo[1] = "";
            			} else {
            				paramInfo[1] = String.valueOf(
            						Math.round(param.getValue() * 
            								100000000.0) / 100000000.0);
            			}
            			dataBarChartOne.add(paramInfo);
            		}
            	}
			}
		} else if (company != null) {
			List<ParameterCollectionScaling> results = 
					company.getResults();
			for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = company.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartOne.add(paramInfo);
				}
			}
			ArrayList<ILocation> locationsOfCompany = company.getLocations();
    		for(ILocation locationOfCompany : locationsOfCompany) {
				List<ParameterCollectionScaling> locationResults = 
						locationOfCompany.getResults();
				for(ParameterCollectionScaling collectionLocation : 
            		locationResults) {
            		for(Parameter param : collectionLocation.getParameters()) {
            			String[] paramInfo = new String[2];
                		paramInfo[0] = locationOfCompany.getName();
            			if(Double.isNaN(param.getValue())) {
            				paramInfo[1] = "";
            			} else {
            				paramInfo[1] = String.valueOf(
            						Math.round(param.getValue() * 
            								100000000.0) / 100000000.0);
            			}
            			dataBarChartOne.add(paramInfo);
            		}
            	}
			}
		} else if (industry != null) {
			List<ParameterCollectionScaling> results = 
					industry.getResults();
			for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = industry.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartOne.add(paramInfo);
				}
			}
			ArrayList<ICompany> companiesOfIndustry = industry.getCompanies();
    		for(ICompany companyOfIndustry : companiesOfIndustry) {
				List<ParameterCollectionScaling> companyResults = 
						companyOfIndustry.getResults();
				for(ParameterCollectionScaling collectionCompany : 
            		companyResults) {
            		for(Parameter param : collectionCompany.getParameters()) {
            			String[] paramInfo = new String[2];
                		paramInfo[0] = companyOfIndustry.getName();
            			if(Double.isNaN(param.getValue())) {
            				paramInfo[1] = "";
            			} else {
            				paramInfo[1] = String.valueOf(
            						Math.round(param.getValue() * 
            								100000000.0) / 100000000.0);
            			}
            			dataBarChartOne.add(paramInfo);
            		}
            	}
			}
		} else if (product != null) {
			List<ParameterCollectionScaling> results = 
					product.getResults();
			for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = product.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartOne.add(paramInfo);
				}
			}
			ArrayList<IFactory> factoriesOfProduct = product.getFactories();
    		for(IFactory factoryOfProduct : factoriesOfProduct) {
				List<ParameterCollectionScaling> factoryResults = 
						factoryOfProduct.getResults();
				for(ParameterCollectionScaling collectionFactory : 
            		factoryResults) {
            		for(Parameter param : collectionFactory.getParameters()) {
            			String[] paramInfo = new String[2];
                		paramInfo[0] = factoryOfProduct.getName();
            			if(Double.isNaN(param.getValue())) {
            				paramInfo[1] = "";
            			} else {
            				paramInfo[1] = String.valueOf(
            						Math.round(param.getValue() * 
            								100000000.0) / 100000000.0);
            			}
            			dataBarChartOne.add(paramInfo);
            		}
            	}
			}
		}
		return dataBarChartOne;
    }
    
    /**
     * To create the parameter information used for the second bar chart. In
     * this bar chart the result of the scalingID is compared to all results
     * that are on the same scaling level.
     * For example if the scalingID belongs to a facility, then this facility 
     * is compared to all other facilities.
     * @param scalingResultID the scalingID belonging to the selected check box.
     * @return <b>dataBarChartTwo</b> a list of Strings with the size 2 called 
     * paramInfo:<br> 
     * dataBarChartOne[0] = paramInfo:<br>
     * paramInfo[0] = name of the manuMethod, facility, factory,...<br>
     * paramInfo[1] = value of the parameter
     */
    private ArrayList<String[]> getDataBarChartTwo(String scalingResultID) {
    	
    	ArrayList<String[]> dataBarChartTwo = new ArrayList<String[]>();
		
		IFacility facility = null;
		IFactory factory = null;
		ILocation location = null;
		ICompany company = null;
		IIndustrialSector industry = null;
		IProduct product = null;
		
		// Try to get the scaling level to which this scalingResultID 
		// belongs.
		try {
			facility = eBenchmarkScaling.getFacility(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			factory = eBenchmarkScaling.getFactory(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			location = eBenchmarkScaling.getLocation(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			company = eBenchmarkScaling.getCompany(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			industry = eBenchmarkScaling.getIndustrialSector(
					scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		try {
			product = eBenchmarkScaling.getProduct(scalingResultID);
		} catch (NoSuchElementException e1) {
			// Nothing to do here
		}
		
		// Test all scaling levels and find out to which this collection 
		// belongs. Then add the appropriate parameter information.
		if(facility != null) {
			
			List<ParameterCollectionScaling> results = 
					facility.getResults();
        	
        	// Iterate through results in order to get access to each 
			// collection within results.
        	for(ParameterCollectionScaling collection : results) {
				
        		// Get the parameter of the scaling level. In this case the
				// parameter of the facility. Therefore iterate through the 
				// collection to get access to each parameter.
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					
					// Add the name.
					paramInfo[0] = facility.getName();
					
					// Add the value.
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			// Get all other facilities. This next step is needed to ensure that 
			// the results of the facilities that are not selected by the user 
			// are shown beneath the results of the selected facility.
			List<IFacility> facilities = eBenchmarkScaling.getFacilities();
			
			if(facilities.size() == 1) {
				return null;
			}
			
			// Iterate through all facilities.
			for(IFacility iterate : facilities) {
				
				// Get the results of the facility.
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				
				// Iterate through the list to get access to each collection.
				for(ParameterCollectionScaling collectionFacility : 
					iterateResults) {
	        		
					// Iterate through the collection to get access to each
					// parameter within the collection.
	        		for(Parameter param : collectionFacility.getParameters()) {
	        			
	        			// Fetch the interesting parameter information and add
	        			// it to dataBarChartTwo.
	        			String[] paramInfo = new String[2];
	        			
	        			// Add the name.
	            		paramInfo[0] = iterate.getName();
	            		
	            		// Add the value.
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		} else if(factory != null) {
			
			List<ParameterCollectionScaling> results = 
					factory.getResults();
        	for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = factory.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			List<IFactory> factories = eBenchmarkScaling.getFactories();
			if(factories.size() == 1) {
				return null;
			}
			for(IFactory iterate : factories) {
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				for(ParameterCollectionScaling collectionFactory : 
					iterateResults) {
	        		for(Parameter param : collectionFactory.getParameters()) {
	        			String[] paramInfo = new String[2];
	            		paramInfo[0] = iterate.getName();
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		} else if(location != null) {
			
			List<ParameterCollectionScaling> results = 
					location.getResults();
        	for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = location.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			List<ILocation> locations = eBenchmarkScaling.getLocations();
			if(locations.size() == 1) {
				return null;
			}
			for(ILocation iterate : locations) {
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				for(ParameterCollectionScaling collectionLocation : 
					iterateResults) {
	        		for(Parameter param : collectionLocation.getParameters()) {
	        			String[] paramInfo = new String[2];
	            		paramInfo[0] = iterate.getName();
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		} else if(company != null) {
			
			List<ParameterCollectionScaling> results = 
					company.getResults();
        	for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = company.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			List<ICompany> companies = eBenchmarkScaling.getCompanies();
			if(companies.size() == 1) {
				return null;
			}
			for(ICompany iterate : companies) {
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				for(ParameterCollectionScaling collectionCompany : 
					iterateResults) {
	        		for(Parameter param : collectionCompany.getParameters()) {
	        			String[] paramInfo = new String[2];
	            		paramInfo[0] = iterate.getName();
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		} else if(industry != null) {
			
			List<ParameterCollectionScaling> results = 
					industry.getResults();
        	for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = industry.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			List<IIndustrialSector> industries = 
					eBenchmarkScaling.getIndustrialSectors();
			if(industries.size() == 1) {
				return null;
			}
			for(IIndustrialSector iterate : industries) {
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				for(ParameterCollectionScaling collectionIndustry : 
					iterateResults) {
	        		for(Parameter param : collectionIndustry.getParameters()) {
	        			String[] paramInfo = new String[2];
	            		paramInfo[0] = iterate.getName();
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		} else if(product != null) {
			
			List<ParameterCollectionScaling> results = 
					product.getResults();
        	for(ParameterCollectionScaling collection : results) {
				for(Parameter param : collection.getParameters()) {
					String[] paramInfo = new String[2];
					paramInfo[0] = product.getName();
					if(Double.isNaN(param.getValue())) {
						paramInfo[1] = "";
					} else {
						paramInfo[1] = String.valueOf(
								Math.round(param.getValue() * 
										100000000.0) / 100000000.0);
					}
					dataBarChartTwo.add(paramInfo);
				}
        	}
			
			List<IProduct> products = eBenchmarkScaling.getProducts();
			if(products.size() == 1) {
				return null;
			}
			for(IProduct iterate : products) {
				List<ParameterCollectionScaling> iterateResults = 
						iterate.getResults();
				for(ParameterCollectionScaling collectionProduct : 
					iterateResults) {
	        		for(Parameter param : collectionProduct.getParameters()) {
	        			String[] paramInfo = new String[2];
	            		paramInfo[0] = iterate.getName();
	        			if(Double.isNaN(param.getValue())) {
	        				paramInfo[1] = "";
	        			} else {
	        				paramInfo[1] = String.valueOf(
	        						Math.round(param.getValue() * 
	        								100000000.0) / 100000000.0);
	        			}
	        			dataBarChartTwo.add(paramInfo);
	        		}
				}
			}
		}
    	return dataBarChartTwo;
	}
		
}
